package com.yanqu.road.logic.pb;

import com.yanqu.road.entity.union.activitygroup.UnionActivityGroup;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroupUnion;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroupUser;
import com.yanqu.road.pb.union.UnionActivityGroupProto;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class UnionActivityGroupPb {

    public static UnionActivityGroupProto.UnionActivityGroupSyncReqMsg.Builder parseUnionActivityGroupSyncReqMsgUpdate(List<UnionActivityGroupUnion> updateUnionList) {
        UnionActivityGroupProto.UnionActivityGroupSyncReqMsg.Builder builder = UnionActivityGroupProto.UnionActivityGroupSyncReqMsg.newBuilder();
        for (UnionActivityGroupUnion union : updateUnionList) {
            builder.addUpdateUnion(parseUnionActivityGroupUnionUpdateMsg(union));
        }
        return builder;
    }

    public static UnionActivityGroupProto.UnionActivityGroupSyncReqMsg.Builder parseUnionActivityGroupSyncReqMsgAll(List<UnionActivityGroup> allGroupList) {
        UnionActivityGroupProto.UnionActivityGroupSyncReqMsg.Builder builder = UnionActivityGroupProto.UnionActivityGroupSyncReqMsg.newBuilder();
        for (UnionActivityGroup group : allGroupList) {
            builder.addAllGroup(parseUnionActivityGroupMsg(group));
        }
        return builder;
    }

    private static UnionActivityGroupProto.UnionActivityGroupMsg.Builder parseUnionActivityGroupMsg(UnionActivityGroup group) {
        UnionActivityGroupProto.UnionActivityGroupMsg.Builder groupBuilder = UnionActivityGroupProto.UnionActivityGroupMsg.newBuilder();
        groupBuilder.setActivityId(group.getActivityId());
        groupBuilder.setCrossServerId(group.getCrossServerId());
        groupBuilder.setGroupId(group.getGroupId());
        groupBuilder.addAllServerId(new ArrayList<>(group.getServerSet()));
        for (UnionActivityGroupUnion union : group.getUnionDataMap().values()) {
            groupBuilder.addUnion(parseUnionActivityGroupUnionMsg(union));
        }
        return groupBuilder;
    }

    private static UnionActivityGroupProto.UnionActivityGroupUnionMsg.Builder parseUnionActivityGroupUnionMsg(UnionActivityGroupUnion union) {
        UnionActivityGroupProto.UnionActivityGroupUnionMsg.Builder unionBuilder = UnionActivityGroupProto.UnionActivityGroupUnionMsg.newBuilder();
        unionBuilder.setUnionUid(union.getUnionUid());
        unionBuilder.setUnionBaseData(UnionBasePb.parseUnionBaseTempMsg(union.getUnionBaseInfo()));
        for (UnionActivityGroupUser user : union.getUserMap().values()) {
            unionBuilder.addUser(parseUnionActivityGroupUserMsg(user));
        }
        unionBuilder.setRank(union.getRank());
        unionBuilder.setLocked(union.getLocked());
        unionBuilder.setAbility(union.getAbility());
        return unionBuilder;
    }

    private static UnionActivityGroupProto.UnionActivityGroupUserMsg.Builder parseUnionActivityGroupUserMsg(UnionActivityGroupUser user) {
        UnionActivityGroupProto.UnionActivityGroupUserMsg.Builder userBuilder = UnionActivityGroupProto.UnionActivityGroupUserMsg.newBuilder();
        userBuilder.setPosition(user.getPosition());
        userBuilder.setServerId(user.getServerId());
        userBuilder.setUserId(user.getUserId());
        return userBuilder;
    }

    public static UnionActivityGroup parseUnionActivityGroup(UnionActivityGroupProto.UnionActivityGroupMsg msg) {
        UnionActivityGroup data = new UnionActivityGroup();
        data.setActivityId(msg.getActivityId());
        data.setCrossServerId(msg.getCrossServerId());
        data.setGroupId(msg.getGroupId());
        data.setServerSet(new HashSet<>(msg.getServerIdList()));
        for (UnionActivityGroupProto.UnionActivityGroupUnionMsg unionMsg : msg.getUnionList()) {
            data.getUnionDataMap().put(unionMsg.getUnionUid(), parseUnionActivityGroupUnion(msg.getActivityId(), msg.getGroupId(), unionMsg));
        }
        return data;
    }

    public static UnionActivityGroupProto.UnionActivityGroupUnionUpdateMsg.Builder parseUnionActivityGroupUnionUpdateMsg(UnionActivityGroupUnion union) {
        UnionActivityGroupProto.UnionActivityGroupUnionUpdateMsg.Builder builder = UnionActivityGroupProto.UnionActivityGroupUnionUpdateMsg.newBuilder();
        builder.setActivityId(union.getActivityId());
        builder.setGroupId(union.getGroupId());
        builder.setUnion(parseUnionActivityGroupUnionMsg(union));
        return builder;
    }

    private static UnionActivityGroupUnion parseUnionActivityGroupUnion(int activityId, int groupId, UnionActivityGroupProto.UnionActivityGroupUnionMsg msg) {
        UnionActivityGroupUnion data = new UnionActivityGroupUnion();
        data.setActivityId(activityId);
        data.setGroupId(groupId);
        data.setUnionUid(msg.getUnionUid());
        data.setUnionBaseInfo(UnionBasePb.parseUnionBaseInfo(msg.getUnionBaseData()));
        for (UnionActivityGroupProto.UnionActivityGroupUserMsg userMsg : msg.getUserList()) {
            data.getUserMap().put(userMsg.getUserId(), parseUnionActivityGroupUser(userMsg));
        }
        data.setRank(msg.getRank());
        data.setLocked(msg.getLocked());
        data.setAbility(msg.getAbility());
        return data;
    }

    public static UnionActivityGroupUnion parseUnionActivityGroupUnion(UnionActivityGroupProto.UnionActivityGroupUnionUpdateMsg msg) {
        UnionActivityGroupProto.UnionActivityGroupUnionMsg unionMsg = msg.getUnion();

        UnionActivityGroupUnion data = new UnionActivityGroupUnion();
        data.setActivityId(msg.getActivityId());
        data.setGroupId(msg.getGroupId());
        data.setUnionUid(unionMsg.getUnionUid());
        data.setUnionBaseInfo(UnionBasePb.parseUnionBaseInfo(unionMsg.getUnionBaseData()));
        for (UnionActivityGroupProto.UnionActivityGroupUserMsg userMsg : unionMsg.getUserList()) {
            data.getUserMap().put(userMsg.getUserId(), parseUnionActivityGroupUser(userMsg));
        }
        data.setRank(unionMsg.getRank());
        data.setLocked(unionMsg.getLocked());
        data.setAbility(unionMsg.getAbility());
        return data;
    }

    private static UnionActivityGroupUser parseUnionActivityGroupUser(UnionActivityGroupProto.UnionActivityGroupUserMsg msg) {
        UnionActivityGroupUser data = new UnionActivityGroupUser();
        data.setServerId(msg.getServerId());
        data.setUserId(msg.getUserId());
        data.setPosition(msg.getPosition());
        return data;
    }

}
